<template>
  <!-- 内容主体区域 -->
  <el-container class="el_container">
    <!-- 内容头部 -->
    <el-header class="el_header">
      <div>
        <img src="../assets/header.png" alt />
        <span>电商后台管理系统</span>
      </div>
      <i class="el-icon-switch-button" @click="logout">&nbsp;退出</i>
    </el-header>
    <!-- 侧边栏和内容区域 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside class="el_aside" :width="isCollpase ? '64px' : '160px'"
                    @mouseenter.native="collapseOpen"
                    @mouseleave.native="collapseClose">
        <div>
        </div>
        <!-- 菜单列表 -->
        <el-menu :collapse="isCollpase" :default-active="activePath" router :collapse-transition="false" background-color="#1b1e22" text-color="#fff" active-text-color="#409EFF" unique-opened>
          <!-- 一级菜单 -->
          <el-submenu :index="item.id + ''" v-for="item in menuList" :key="item.id">
            <template slot="title">
              <i :class="iconsObjs[item.id]"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级菜单 -->
            <el-menu-item :index="'/' + subItem.path" v-for="subItem in item.children" :key="subItem.id">
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span @click="saveActivePath('/' + subItem.path)">{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main class="el_main">
        <!-- 放入内容的router占位符 -->
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
export default {
  created () {
    this.getMenusList()
    // 页面刷新保存之前的页面链接
    this.activePath = window.sessionStorage.getItem('activePath')
  },
  data () {
    return {
      menuList: [],
      // 一级菜单icon
      iconsObjs: {
        125: 'el-icon-user-solid',
        103: 'el-icon-view',
        101: 'el-icon-present',
        102: 'el-icon-notebook-2',
        145: 'el-icon-s-data'
      },
      // 是否折叠
      isCollpase: true,
      activePath: ''
    }
  },
  mounted () {
    document.title = '电商管理系统'
  },
  methods: {
    logout () {
      // 清除token
      window.sessionStorage.clear()
      // 跳转登录界面
      this.$router.push('/login')
    },
    async getMenusList () {
      const { data: res } = await this.$http.get('menus')
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg)
      this.menuList = res.data
    },
    // 改变侧边栏
    collapseOpen () {
      this.isCollpase = false
    },
    collapseClose () {
      this.isCollpase = true
    },
    // 页面刷新还能回到之前的界面
    saveActivePath (activePath) {
      // 存储到sessionStorage中
      window.sessionStorage.setItem('activePath', activePath)
      this.activePath = activePath
    }
  }
}
</script>

<style lang="less" scoped>
.el_container {
  height: 100%;
}
.el_header {
  background-color: #1b1e22;
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: #fff;
  font-size: 18px;
  > div {
    display: flex;
    align-items: center;
    span {
      padding-left: 10px;
    }
    img {
      width: 40px;
      height: 40px;
    }
  }
  > i {
    cursor: pointer;
    font-size: 15px;
  }
}
.el_aside {
  background-color: #1b1e22;
  .el-menu{
    border-right: none;
  }
}
.el_main {
  background-color: #ffffff;
}
</style>
